/*
* Copyright 2010 Competentum group
*
* Licensed under the GNU General Public License, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.gnu.org/licenses/gpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.competentum.data.parser
{
	import com.competentum.builders.BindsBuilder;
	import com.competentum.builders.DataBuildersFactory;
	import com.competentum.builders.EmployeesBuilder;
	import com.competentum.builders.ProjectsBuilder;
	import com.competentum.vos.AbstractItemVO;
	import com.competentum.vos.BindVO;
	import com.competentum.vos.EmployeeVO;
	import com.competentum.vos.ProjectVO;
	import com.competentum.lib.parser.IDataParser;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	public class CSVParser extends EventDispatcher implements IDataParser
	{
		private const TAB:String = '\t';
		private const EOL:String = '\n';
		
		public function CSVParser(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		public function parse(data:String, scope:*):void
		{
			var employeesBuilder:EmployeesBuilder = DataBuildersFactory.getDataBuilder(EmployeesBuilder.TYPE, scope) as EmployeesBuilder;
			var projectsBuilder:ProjectsBuilder = DataBuildersFactory.getDataBuilder(ProjectsBuilder.TYPE, scope) as ProjectsBuilder;
			var bindsBuilder:BindsBuilder = DataBuildersFactory.getDataBuilder(BindsBuilder.TYPE, scope) as BindsBuilder;
			
			employeesBuilder.removeAllItems();
			projectsBuilder.removeAllItems();
			bindsBuilder.removeAllItems();
			
			var cursor:CSVCursor = new CSVCursor(data);
			
			var projects:Vector.<ProjectVO> = new Vector.<ProjectVO>();
			var project:ProjectVO;
			cursor.forward();
			do
			{
				cursor.forward();
				project = projectsBuilder.createItem(cursor.currentSlice, 'no', 0, null, null, false);
				projects.push(project);
			}
			while (!cursor.lastInLine);
			
			cursor.forward(2);
			for (var i:int = 0; i < projects.length; i ++ )
			{
				cursor.forward();
				project = projects[i];
				project.hours = int(cursor.currentSlice);
				projectsBuilder.addItem(project);
			}
			
			while (!cursor.lastInFile)
			{
				cursor.forward();
				var name:String = cursor.currentSlice;
				cursor.forward();
				var employee:AbstractItemVO = employeesBuilder.createItem(name, 'no', int(cursor.currentSlice));
				for (var j:int = 0; j < projects.length; j ++ )
				{
					cursor.forward();
					if (cursor.currentSlice != '')
					{
						bindsBuilder.createItem(employee, projects[j], int(cursor.currentSlice));
					}
				}
			}
		}
		
		public function export(scope:*):String
		{
			var employeesBuilder:EmployeesBuilder = DataBuildersFactory.getDataBuilder(EmployeesBuilder.TYPE, scope) as EmployeesBuilder;
			var projectsBuilder:ProjectsBuilder = DataBuildersFactory.getDataBuilder(ProjectsBuilder.TYPE, scope) as ProjectsBuilder;
			var bindsBuilder:BindsBuilder = DataBuildersFactory.getDataBuilder(BindsBuilder.TYPE, scope) as BindsBuilder;
			
			var output:String;
			var i:int;
			output = TAB + TAB;
			for (i = 0; i < projectsBuilder.itemsCount; i ++ )
			{
				output += (projectsBuilder.getItemByIndex(i) as ProjectVO).name;
				if (i < projectsBuilder.itemsCount - 1)
				{
					output += TAB;
				}
				else
				{
					output += EOL;
				}
			}

			output += TAB + TAB;
			for (i = 0; i < projectsBuilder.itemsCount; i ++ )
			{
				output += (projectsBuilder.getItemByIndex(i) as ProjectVO).hours;
				if (i < projectsBuilder.itemsCount - 1)
				{
					output += TAB;
				}
				else
				{
					output += EOL;
				}
			}
			
			for (var eIndex:int = 0; eIndex < employeesBuilder.itemsCount; eIndex ++ )
			{
				var employee:EmployeeVO = employeesBuilder.getItemByIndex(eIndex) as EmployeeVO;
				output += employee.name + TAB + employee.hours + TAB;
				
				for (i = 0; i < projectsBuilder.itemsCount; i ++ )
				{
					var bind:BindVO = bindsBuilder.getBindForPair(employee, projectsBuilder.getItemByIndex(i) as ProjectVO);
					if (bind)
					{
						output += bind.hoursReserved;
					}
					if (i < projectsBuilder.itemsCount - 1)
					{
						output += TAB;
					}
					else
					{
						if (eIndex < employeesBuilder.itemsCount - 1)
						{
							output += EOL;
						}
					}
				}
			}
			return output;
		}
	}
}